package com.heima.a07_demo.server.service.impl;

import com.heima.a07_demo.common.utils.SmsCodeUtils;
import com.heima.a07_demo.server.service.CommonService;
import jakarta.mail.Message;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class CommonServiceImpl implements CommonService {
    @Autowired
    JavaMailSender mailSender;


    @Override
    public String sendMails() {

        // Do the business calculations...
        // Call the collaborators to persist the order...
        String code = SmsCodeUtils.generateNumericCode(6);



        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                //接受邮箱地址
                mimeMessage.setRecipient(Message.RecipientType.TO,
                        new InternetAddress("2042386965@qq.com"));
                //发送邮箱地址
                mimeMessage.setFrom(new InternetAddress("2042386965@qq.com"));
                //邮箱内容
                mimeMessage.setText("您的手机验证码为" + code );
            }
        };

        try {
            this.mailSender.send(preparator);
            return code;
        }
        catch (MailException ex) {
            // simply log it and go on...
            System.err.println(ex.getMessage());
            return null;
        }
    }
}
